<template>
	<view class="index">
		<button class="clinic" hover-class='none' plain="true" open-type="getUserInfo" @getuserinfo="jumpClinic(6)">
			<image :src="clinic" mode="scaleToFill" style="width: 230rpx; height: 230rpx;"></image>
			<text>我是诊所</text>
		</button>
		<button class="doctor" hover-class='none' plain="true" open-type="getUserInfo" @getuserinfo="jumpDoctor(9)">
			<image :src="doctor" mode="scaleToFill" style="width: 230rpx; height: 230rpx;"></image>
			<text>我是医生</text>
		</button>
	</view>
</template>

<script>
	import TIM from 'tim-wx-sdk';
	export default {
		data() {
			return {
				clinic: "/static/image/index/clinic.png",
				doctor: "/static/image/index/doctor.png"
			};
		},
		methods: {
			async jumpDoctor(num) {
				this.getUseInfo(num);
			},
			async jumpClinic(num) {
				this.getUseInfo(num);
			},
			// 获取用户信息
			async getUseInfo(num) {
				uni.getUserInfo({
					provider: 'weixin',
					success: res => {
						if (!res.userInfo) {
							uni.redirectTo({
								url: "../clinic/addClinic2/addClinic2"
							});
							return;
						};
						this.$store.commit("storePersonalInformation", res.userInfo);
						let openid = this.$store.state.openid;
						// 得到用户类型
						this.getUserType(openid, res.userInfo.nickName, res.userInfo.avatarUrl, num);
					}
				});
			},
			// 得到用户类型
			async getUserType(openid, nickName, avatarUrl, num) {
				let selfs = this;
				let res = await this.$myrequest({
					url: "User/getUser",
					method: "POST",
					data: {
						"openid": openid,
						"nick_name": nickName,
						"avatar_url": avatarUrl,
					},
				});
				this.$store.commit("storePersonalInformation", res.data.data);
				// 即时通讯的注册
				uni.request({
					url: `https://chat.assessgeek.com/api/Im/addIMUser?identifier=${res.data.data.openid}&nick=${res.data.data.service_title}&avatar_url=${res.data.data.img_url}`,
					success(imMsg) {
						if (imMsg.data.data.ErrorCode == 0) {
							uni.request({
								url: `https://chat.assessgeek.com/api/Im/getUserSig?userid=${res.data.data.openid}`,
								success(imsig) {
									if (imsig.data.code == 200) {
										selfs.$store.commit("storeImsig", imsig.data.data);
										let options = {
											SDKAppID: 1400388580
										};
										selfs.globalData.tim = TIM.create(options);
										selfs.globalData.tim.setLogLevel(0);
										let openid = selfs.$store.state.userinfo.openid;
										let imsigs = imsig.data.data;
										let loginPromise = selfs.globalData.tim.login({
											userID: openid,
											userSig: imsigs
										}).then((imResponse) => {
											if (imResponse.data.repeatLogin === true) {
												console.log(imResponse.data.errorInfo);
											}
										}).catch((err) => {
											console.warn(err);
										});
										// ======================
									}
								}
							});
						};
					}
				});

				// ====================================================================
				this.jumpPage(res.data.identity, res, num);
			},
			// 判定页面跳转
			async jumpPage(identity, res, num) {
				if (identity == 2) {
					uni.switchTab({
						url: "../clinic/homePage3/homePage3"
					})
				} else if (identity == 3) {
					if (res.data.data.is_status == "2") {
						uni.redirectTo({
							url: "../doctor/examine4/examine1/examine1"
						})
					} else if (res.data.data.is_status == 1) {
						uni.redirectTo({
							url: "../doctor/my5/profile1/profile1"
						});
					} else if (res.data.data.is_status == 3) {
						uni.redirectTo({
							url: "../doctor/examine4/examineAdopt2/examineAdopt2"
						});
					};
				} else {
					if (num == 6) {
						uni.redirectTo({
							url: "../clinic/guidePage1/guidePage1"
						})
					} else {
						uni.redirectTo({
							url: "../doctor/new1/new1"
						})
					};
				};
			}

		}
	}
</script>
<style lang="scss">
	.index {
		background-color: RGBA(219, 244, 255, 1);
		box-shadow: 3px 4px 9px 0px rgba(0, 0, 0, 0.05) inset;

		button {
			height: 322rpx;
			border: none;
			position: relative;

			>text {
				position: absolute;
				bottom: 0;
				left: 50%;
				transform: translateX(-50%);
			}
		}

		.clinic {
			margin-top: 230rpx;
		}

		.doctor {
			margin-top: 100rpx;
		}
	}
</style>
